<?php
/*----------------------------------------------------------------------
 * 小帅订单逻辑
----------------------------------------------------------------------*/

namespace Common\Logic;
use Think\Controller;
use Think\Log;

class XsSyncLogic
{
    /**
     * 保存影院的订单，生成支付二维码
     * @param unknown $params
     */
    public function handleOrder($params)
    {
        // 验证参数正确性
        if (!D('Common/XsCommon')->isHostNameValid($params['host_name'])) {
            $result['error_code'] = 1;
            $result['message'] = '影院参数不正确';
            return $result;
        }
        
        // 验证参数正确性
        if (empty($params['video_id'])) {
            $result['error_code'] = 1;
            $result['message'] = '影片参数不正确';
            return $result;
        }
        if (empty($params['hall_id'])) {
            $result['error_code'] = 1;
            $result['message'] = '影厅不正确';
            return $result;
        }
        
        //保存订单到数据库
        $order['cinema_order_sn'] = $params['order_sn'];
        $order['cinema_create_time'] = $params['create_time'];
        $order['video_id'] = $params['video_id'];
        $order['host_name'] = $params['host_name'];
        $order['video_name'] = $params['video_name'];
        $order['hall_id'] = $params['hall_id'];
        $order['hall_no'] = $params['hall_no'];
        $order['price'] = $params['price'];
        $res = D('Common/XsOrder')->saveOrder($order);
        if (!$res) {
            $result['error_code'] = 1;
            $result['message'] = '下单失败, 请稍后再试';
            return $result;
        }
        
        //向小帅发起支付请求
        $_rtn = D('Common/XsOrder')->callPayHandle($res['id']);
        //请求失败
        if ($_rtn['error_code']) {
            //删除订单
            D('Common/XsOrder')->deleteOrder($res['net_order_sn']);
        
            $result['error_code'] = 1;
            $result['message'] = '云端消息:' . $_rtn['message'];
            return $result;
        }
        
        $result['error_code'] = 0;
        $result['data'] = $_rtn;
        return $result;
    }
    
    /**
     * 支付回调
     * @param unknown $params
     */
    public function payCallback($params)
    {
        $order_sn = $params["order_sn"];
        $order = D("Common/XsOrder")->getDetailByOrderSN($order_sn);
        
        // 验证参数正确性
        if (empty($order)) {
            $result['error_code'] = 1;
            $result['message'] = '订单参数不正确';
            return $result;
        }
        
        //获取远程ip
        $map['host_name'] = $order['host_name'];
        $host_ip = M('cinema_host')->where($map)->getField('host_ip');
        
        try{
            //远程方法
            $params['class'] = "XsOrder";
            $params['method'] = "notify";
        
            //需要传递的参数
            $params['host_ip'] = $host_ip;
            $params['order_sn'] = $order['cinema_order_sn'];
            $params['pay_method'] = $order['pay_method'];
            $params['pay_time'] = $order['pay_time'];
            $params['pay_result_code'] = ($order['pay_status'] == 1) ? 'SUCCESS' : 'FAIL';
        
            $res = callCinemaInterfaceData($params);

//            file_put_contents('./Application/Runtime/Logs/115_to_130.log', json_encode($host_ip.'|'.$res), FILE_APPEND );

            //操作出错
            if (!isset($res['data']['error_code']) || $res['data']['error_code']) {
                if(!isset($res['data']['error_code'])) {
                    $result['error_code'] = 2;
                    $result['message'] = "host {$host_ip} response:" . $res['code'];
                } else {
                    $result['error_code'] = 1;
                    $result['message'] = $res['message'];
                }
                
            } else {
                $result['error_code'] = 0;
            }
            return $result;
        } catch ( \Exception $e) {
            $error = $e->getMessage();
            return array('error_code'=>1, 'message'=>$error);
        }
    }
    
    /**
     * 设置发票信息
     * @param unknown $params
     */
    public function handleInvoice($params)
    {
        // 验证参数正确性
        $host_name = $params['host_name'];
        if (!D('Common/XsCommon')->isHostNameValid($host_name)) {
            $result['error_code'] = 1;
            $result['message'] = '影院参数不正确';
            return $result;
        }
        $cinema_order_sn = $params['order_sn'];
        if (empty($cinema_order_sn)) {
            $result['error_code'] = 1;
            $result['message'] = '参数不正确';
            return $result;
        }
        
        $need_invoice = $params['invoice'] ? 1 : 0;
        //保存发票信息
        $res = D('Common/XsOrder')->setInvoice($host_name, $cinema_order_sn, $need_invoice);
        
        if ($res !== false) {
            $result['error_code'] = 0;
            $result['message'] = '设置成功';
        } else {
            $result['error_code'] = 1;
            $result['message'] = '设置失败';
        }
        
        return $result;
    }
    
    /**
     * 退款申请
     * @param unknown $params
     * @return string
     */
    public function handleRefund($params)
    {
        // 验证参数正确性
        $host_name = $params['host_name'];
        if (!D('Common/XsCommon')->isHostNameValid($host_name)) {
            $result['error_code'] = 1;
            $result['message'] = '影院参数不正确';
            return $result;
        }
        $cinema_order_sn = $params['order_sn'];
        if (empty($cinema_order_sn)) {
            $result['error_code'] = 1;
            $result['message'] = '参数不正确';
            return $result;
        }
        
        //向小帅发起退款申请
        $_rtn = D('Common/XsOrder')->callRefundHandle($host_name, $cinema_order_sn);
        //请求失败
        if ($_rtn['error_code']) {
            $result['error_code'] = 1;
            $result['message'] = '云端消息:' . $_rtn['message'];
        } else {
            $result['error_code'] = 0;
            $result['message'] = '请求成功';
        }
        
        return $result;
    }
    
    /**
     * 向小帅读取数据
     * @param unknown $params
     */
    public function handleCloundConfig($params)
    {
        try{
            //远程方法
            $params['class'] = "XsSync";
            $params['method'] = "getConfig";
        
            $res = callRemoteInterfaceData(C("HOST_XS_WEB"), $params, "/Xs/Interface");
            //操作出错
            if (!isset($res['data']['error_code']) || $res['data']['error_code']) {
                if(!isset($res['data']['error_code'])) {
                    $res['data']['error_code'] = 2;
                    $res['data']['message'] = "can not connect xs web server";
                }
                
                return array('error_code'=>$res['data']['error_code'], 'message'=>$res['data']['message']);
            } else {
                return $res['data'];
            }
        } catch ( \Exception $e) {
            $error = $e->getMessage();
            return array('error_code'=>1, 'message'=>$error);
        }
    }

	/**
     * 向小帅读取数主题信息
     * @param unknown $params
     */
    public function recommendInfo($params)
    {
        try{
            //远程方法
            $params['class'] = "XsSync";
            $params['method'] = "recommendInfo";
        
            $res = callRemoteInterfaceData(C("HOST_XS_WEB"), $params, "/Xs/Interface");
            //操作出错
            if (!isset($res['data']['error_code']) || $res['data']['error_code']) {
                if(!isset($res['data']['error_code'])) {
                    $res['data']['error_code'] = 2;
                    $res['data']['message'] = "can not connect xs web server";
                }
                
                return array('error_code'=>$res['data']['error_code'], 'message'=>$res['data']['message']);
            } else {
                return $res['data'];
            }
        } catch ( \Exception $e) {
            $error = $e->getMessage();
            return array('error_code'=>1, 'message'=>$error);
        }
    }

	/**
     * 获取小帅播放模式
     * @param unknown $params
     */
    public function playModule($params)
    {
        try{
            //远程方法
            $params['class'] = "XsSync";
            $params['method'] = "playModule";
        
            $res = callRemoteInterfaceData(C("HOST_XS_WEB"), $params, "/Xs/Interface");
            //操作出错
            if (!isset($res['data']['error_code']) || $res['data']['error_code']) {
                if(!isset($res['data']['error_code'])) {
                    $res['data']['error_code'] = 2;
                    $res['data']['message'] = "can not connect xs web server";
                }
                
                return array('error_code'=>$res['data']['error_code'], 'message'=>$res['data']['message']);
            } else {
                return $res['data'];
            }
        } catch ( \Exception $e) {
            $error = $e->getMessage();
            return array('error_code'=>1, 'message'=>$error);
        }
    }

	/**
     * 更新云端的价格设置
     * @param unknown $params
     */
    public function syncPrice($params)
    {
        try{
            //远程方法
            $params['class'] = "XsSync";
            $params['method'] = "syncPrice";
            $res = callRemoteInterfaceData(C("HOST_XS_WEB"), $params, "/Xs/Interface");
            //操作出错
            if (!isset($res['data']['error_code']) || $res['data']['error_code']) {
                if(!isset($res['data']['error_code'])) {
                    $res['data']['error_code'] = 2;
                    $res['data']['message'] = "can not connect xs web server";
                }
                
                return array('error_code'=>$res['data']['error_code'], 'message'=>$res['data']['message']);
            } else {
                return $res['data'];
            }
        } catch ( \Exception $e) {
            $error = $e->getMessage();
            return array('error_code'=>1, 'message'=>$error);
        }
    }


    public function syncVideoRecord($data){
        try{
            //远程方法
            $params['class'] = "XsSync";
            $params['method'] = "syncVideoRecord";

            $params['video_record']=$data['video_record'];

            $res = callRemoteInterfaceData(C("HOST_XS_WEB"), $params, "/Xs/Interface");

            //操作出错
            if (!isset($res['data']['error_code']) || $res['data']['error_code']) {
                if(!isset($res['data']['error_code'])) {
                    $res['data']['error_code'] = 2;
                    $res['data']['message'] = "can not connect xs web server";
                }

                return array('error_code'=>$res['data']['error_code'], 'message'=>$res['data']['message']);
            } else {
                return $res['data'];
            }

        } catch ( \Exception $e) {
            $error = $e->getMessage();
            return array('error_code'=>1, 'message'=>$error);
        }
    }


    //获取云端的酒店状态
    public function syncHotelStatus($params)
    {
        $file="./Application/Runtime/Logs/hotelStatus.log";
        Log::write(date('Y-m-d H:i:s').' | '.$params['host_name'].' | '.'function start...'."\r\n",'INFO','',$file);

        try{
            //远程方法
            $params['class'] = "XsSync";
            $params['method'] = "syncHotelStatus";

            $res = callRemoteInterfaceData(C("HOST_XS_WEB"), $params, "/Xs/Interface");

            $msg=date('Y-m-d H:i:s').' | '.$params['host_name'].' | '.json_encode($res);

            if (!isset($res['data']['error_code']) || $res['data']['error_code']) {
                if(!isset($res['data']['error_code'])) {
                    $res['data']['error_code'] = 2;
                    $res['data']['message'] = "can not connect xs web server";
                }

                Log::write($msg.' | '. $res['data']['message']."\r\n",'ERROR','',$file);
                return array('error_code'=>$res['data']['error_code'], 'message'=>$res['data']['message']);
            } else {

                Log::write($msg.' | success...'."\r\n",'INFO','',$file);
                return $res['data'];
            }
        } catch ( \Exception $e) {
            $error = $e->getMessage();

            Log::write($error."\r\n",'WARN','',$file);
            return array('error_code'=>1, 'message'=>$error);
        }

    }

    /**
     * 获得云端的广告内容
     * @param $params
     * @return array
     */
    public function getAdv($params){
        try{
            //远程方法
            $params['class'] = "XsSync";
            $params['method'] = "getAdv";

            $res = callRemoteInterfaceData(C("HOST_XS_WEB"), $params, "/Xs/Interface");
            //操作出错
            if (!isset($res['data']['error_code']) || $res['data']['error_code']) {
                if(!isset($res['data']['error_code'])) {
                    $res['data']['error_code'] = 2;
                    $res['data']['message'] = "can not connect xs web server";
                }

                return array('error_code'=>$res['data']['error_code'], 'message'=>$res['data']['message']);
            } else {
                return $res['data'];
            }
        } catch ( \Exception $e) {
            $error = $e->getMessage();
            return array('error_code'=>1, 'message'=>$error);
        }

}
}
